<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ include file="/commons/taglibs.jsp" %>
<HTML>
    <HEAD>
        <TITLE> MzTreeView 2 </TITLE>
        <meta http-equiv='Content-Type' content='text/html; charset=uft-8'>
        <SCRIPT src="${ctx}/js/ui/scripts/jsframework.js" type=text/javascript></SCRIPT>
    </HEAD>

    <BODY>
    	<%-- --%>
        <input type="button" value="expandAll" onclick="tree.expandAll('-1')" />
        <input type="button" value="collapseAll" onclick="tree.collapseAll('-1')" />
        <input type="button" value="focusPath" onclick="" />
        <input type="button" value="append" onclick="tree.appendNode('1', 'text: new node;')">
        <input type="button" value="添加新节点" onclick="addNewNodes();">
        <input type="button" value="获取选中值" onclick="PushCheckItems();">
        
		<script language="javascript" type="text/javascript">
			
			 function checkbox(){    //用于返回选择的节点的个数
                var x = new Array();
                for(var i in tree.nodes){
                    if(tree.nodes[i].checked){
                        x.push(tree.nodes[i].id);
                    }
                }
                return x;
            }
            
            function addNewNodes(){
            	//alert(tree.selectedNode);
            	var selectedId = tree.selectedNode.id;
            	alert(selectedId);
                //var x = checkbox();    //调用上面的函数
                //alert(x.length);显示选择节点的个数
                if(selectedId == null){
                    alert("请选择了节点之后再进行增加！");
                }else{            
                    tree.appendNode('1','40000','text:要设置id值;');
                }
            } 
		
			//SetCheckItems用来设置选中节点，即从数据库读取权限后，勾选相应的节点,根据字符串来进行修改。
			function SetCheckItems(items)
			{
				//alert(items);
			    var nodes = tree.nodes;//获取到的只是一级的
			    //console.debug("nodes*********"+nodes.length);
			    var IDs = items.split(",");
			    for(i=0;i<IDs.length;i++){
			        //console.debug("id:  "+IDs[i].toString());
			        for(var j in nodes) {
			        	//alert(nodes[j].id);
			        	//console.debug("node id:  "+nodes[j].id);
			        	//console.debug(nodes[j].id == IDs[i].toString());
			            if(nodes[j].id == IDs[i].toString()) {
			                nodes[j].check(true);
			                nodes[j].upCheck();
			            }
			        }
			    }
			}
			
			//PushCheckItems用来获取当前需要保存的（已勾选）的所有节点集，惟逗号隔开
			//此方法将在提交按钮的OnClientClick事件中执行
			//如何获取选中值
			function PushCheckItems()
			{
			    var IDs="";
			    var nodes = tree.nodes;
			    for(var i in nodes) {
			        if(nodes[i].checked){
			            text = nodes[i].text;
			            IDs +=nodes[i].id + ",";
			        }
			    }
			    //alert(IDs);
			    //
			    //var hf = document.getElementById("hfChecked");
			    //hf.value = IDs;
			}
		</script>
        
		<script language="JavaScript" type="text/javascript"> 
                <%=request.getAttribute("treeData")%>
              	setTimeout("SetCheckItems('1913,1909,')",300); 
              	//集焦点，修改树的时候
              	//alert(''+${parentId}+'');
				tree.focusNodeByPath('${parentId}');
         </script>
    </BODY>
</HTML>